'use strict';

const db = uniCloud.database()
exports.main = async (event, context) => {
	let { uuid, value, mode } = event;
	
	if(mode === 0) {
		if(value > 0){
			const res = await db.collection('users').where({
				uuid
			}).update({
			  "studyNewTimes": db.command.inc(value)
			})
		} else if(value < 0) {
			const getRes = await db.collection('users').where({
				uuid
			}).get()
			const currentValue = getRes.data[0].studyNewTimes ?? 0
			
			if(value + currentValue <= 0) {
				const res = await db.collection('users').where({
					uuid
				}).update({
				  "studyNewTimes": 0
				})
			} else {
				const res = await db.collection('users').where({
					uuid
				}).update({
				  "studyNewTimes": db.command.inc(value)
				})
			}
		}
	} else {
		if(value > 0){
			const res = await db.collection('users').where({
				uuid
			}).update({
			  "reviewTimes": db.command.inc(value)
			})
		} else if(value < 0) {
			const getRes = await db.collection('users').where({
				uuid
			}).get()
			const currentValue = getRes.data[0].reviewTimes ?? 0
			
			if(value + currentValue <= 0) {
				const res = await db.collection('users').where({
					uuid
				}).update({
				  "reviewTimes": 0
				})
			} else {
				const res = await db.collection('users').where({
					uuid
				}).update({
				  "reviewTimes": db.command.inc(value)
				})
			}
		}
	}
	


};
